<template>
  <div class="dictionaryController">
    <div class="title"><h3>自定义选项值</h3></div>

    <div>
      <label
        >新增选项值:
        <el-input v-model="dic_Name"></el-input>
      </label>
    </div>
    <div class="values">
      <div class="title">已有选项值:</div>
      <div class="hasValue">
        <div class="item" v-for="(item, index) in list" :key="index">
          <span>{{ item.dic_Name }}</span
          ><span class="del" @click="del($event)">x</span>
        </div>
      </div>
    </div>
    <div class="fix" @click="tofixzidingyi()">自定义选项修改</div>
    <div class="btn">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { mapState } from "vuex";
export default {
  data() {
    return {
      id: "",
      dic_Name: "",
      type: "",
      typeList: [],
      current: "",
      list: [],
    };
  },
  computed: {
    ...mapState("userManage", ["customerType", "customerOrigin"]),
  },
  methods: {
    ...mapActions([
      "userManage/addDicActions",
      "userManage/delDicActions",
      "userManage/getDicListActions",
    ]),

    tofixzidingyi() {
      this.$router.push("../home/fixzidingyi?val=" + this.type);
    },
    add() {
      console.log(this.type);
      this["userManage/addDicActions"]({
        dic_Name: this.dic_Name,
        parentDic: {
          dic_Name: this.type,
        },
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "新增成功",
          });
          this["userManage/getDicListActions"](this.type)
            .then(() => {
              console.log("返回客户类型数据成功");
              if (this.type === "客户类型") {
                this.list = this.customerType;
              } else {
                this.list = this.customerOrigin;
              }
            })
            .catch(() => {
              console.log("请求失败");
            });
          this.dic_Name = "";
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "新增失败",
          });
        });
    },
    del(e) {
      let dic_Name = e.path[1].innerText.substr(
        0,
        e.path[1].innerText.length - 2
      );
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this["userManage/delDicActions"]({
          dic_Name,
        })
          .then(() => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this["userManage/getDicListActions"](this.type).then(() => {
              if (this.type === "客户类型") {
                this.list = this.customerType;
              } else {
                this.list = this.customerOrigin;
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "删除失败",
            });
          });
      });
    },
  },
  created() {
    console.log(this.$route.query.id);

    if (this.$route.query.id === "1") {
      this.type = "客户类型";
      this.list = this.customerType;
    } else {
      this.type = "客户来源";
      this.list = this.customerOrigin;
    }
  },
};
</script>

<style lang="scss" scoped>
.title {
  width: 500px;
  height: 50px;
  h3 {
    text-align: center;
    line-height: 50px;
  }
}
.dictionaryController {
  width: 500px;
  margin: 20px auto 50px;
  padding: 20px;
}
.add {
  margin-left: 20px;
}
.el-input {
  width: 300px;
}
label {
  display: block;
  margin: 30px auto;
}
.values {
  display: flex;
  justify-content: flex-start;
  .title {
    width: 100px;
  }
}
.item {
  width: 250px;
  padding: 5px;
  border-bottom: 1px dashed #ccc;
}
.btn {
  margin-left: 90px;
  margin-top: 40px;
}
.del {
  color: red;
  padding-left: 10px;
  cursor: pointer;
}
.item {
  display: flex;
  justify-content: space-between;
}
.fix {
  display: inline;
  background-color: red;
  color: white;
  margin-left: 90px;
  cursor: pointer;
}
</style>